import React, {Component} from "react"
import {Route, Redirect} from "react-router-dom"
import {connect} from "react-redux"

class AuthRoute extends Component {
    render() {
        const {component: Component, login, ...rest} = this.props
        return <Route
            {...rest}
            render={
                props =>
                    (
                        login
                        ? <Component {...props} />
                        : <Redirect to={{pathname: '/login', from: props.location}}/>
                    )
            }
        />
    }
}

const mapState = state => ({
    login: state.global.isLogin,
})

export default AuthRoute = connect(mapState, null)(AuthRoute)


